package com.baosight.sczx.zm.ch.service;
import java.util.HashMap;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.Department;
import com.baosight.sczx.common.zm.cInterface.cs.InitialData;
import com.baosight.sczx.zm.cg.domain.ZMCGRecord;
import com.baosight.sczx.zm.ch.domain.ZMCHPlanValidateData;
/**
 * 采煤
 */
public class ServiceZMCH91 extends ServiceEPBase {
	/**
	 * 初始进入方法
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCH91</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2013-6-25 上午14:37:20 王开兵
	 * </p>
	 *
	 */
	@Override
	public EiInfo initLoad(EiInfo inInfo) 
	{
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		String fdForeignId = inInfo.getString("keyId");
		EiInfo outInfo = this.query(inInfo);
		outInfo.setCell("result_add", 0, "fdForeignId", fdForeignId);
		return outInfo;
	}
	
	/**
	 * 查询方法
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCH91</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2013-6-25 上午14:47:20 王开兵
	 * </p>
	 *
	 */
	@Override
	public EiInfo query(EiInfo inInfo) 
	{
		String fdForeignId = inInfo.getString("fdForeignId");
		StaticTools.setQueryParameters(inInfo, "fdForeignId", fdForeignId, true);
		String fdMiningId = inInfo.getString("fdMiningId");
		StaticTools.setQueryParameters(inInfo, "fdMiningId", fdMiningId, true);
		inInfo.setCell("inqu_status", 0, "fdForeignId", fdForeignId);
		EiInfo outInfo = super.query(inInfo, "ZMCHPlanValidateData.query", new ZMCHPlanValidateData());
		
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.ch.planValidateData", true));//验收类型
//		outInfo.addBlock(Department.getInstance().getConstructionUnit("cmes.cp.department", "orgCode", "orgName", false));
//		outInfo.addBlock(Department.getInstance().getMiningDepts("cmes.cp.department", "orgCode", "orgName", false,UserSession.getInSessionProperty("miningCode").toString()));
		outInfo.addBlock(Department.getInstance().getMiningDepts("cmes.cp.department", "orgCode", "orgName", false,UserSession.getInSessionProperty("miningCode").toString()));
//		outInfo.addBlock(Department.getInstance().getConstructionUnit("cmes.cp.department", "orgCode", "orgName", false));//部门

		
		outInfo.set("fdCreatorId", UserSession.getUserId());//登录人的默认值
		outInfo.set("fdCreator", UserSession.getInSessionProperty("UserCname"));
		outInfo.set("fdCreateDepartmentId", UserSession.getInSessionProperty("orgCode"));//单位的默认值		
		outInfo.set("fdCreateDepartment", UserSession.getInSessionProperty("orgName"));//单位的默认值
		outInfo.set("fdMiningId", UserSession.getInSessionProperty("miningCode"));//矿ID的默认值
		outInfo.set("fdMiningName", UserSession.getInSessionProperty("miningName"));//矿名称的默认值
		
		return outInfo;
	}


	/**
	 * 新增方法
	 * <p>Function: insert</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCBCoalseam</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2013-6-25上午11:34:17 王开兵
	 * </p>
	 *
	 */
	@Override
	public EiInfo insert(EiInfo inInfo) 
	{
		EiInfo out=new EiInfo(); 
		ZMCHPlanValidateData _dom = new ZMCHPlanValidateData();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			
			Map<String, String> params = new HashMap<String, String>();

			_dom.setFdKeyId(TablePk.getInstance().getNextValue("T_CH_PLANVALIDATEDATA"));
		    this.dao.insert("ZMCHPlanValidateData.insert", _dom);
          }

		inInfo.set("fdForeignId",eiBlock.getCell( 0, "fdForeignId"));
		inInfo.set("fdMiningId",eiBlock.getCell( 0, "fdMiningId"));
	 	out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out; 		
				
 }

	/**
	 * 修改方法
	 * <p>Function: update</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCBCoalseam</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2013-6-24上午13:34:17 王开兵
	 * </p>
	 *
	 */
	@Override
	public EiInfo update(EiInfo inInfo) 
	{
		EiInfo out=new EiInfo();
		ZMCHPlanValidateData _dom = new ZMCHPlanValidateData();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			
			Map<String, String> params = new HashMap<String, String>();
			params.put("fdKeyId", _dom.getFdKeyId());

		    this.dao.update("ZMCHPlanValidateData.update", _dom);			
		}
		inInfo.set("fdForeignId",eiBlock.getCell( 0, "fdForeignId"));
		inInfo.set("fdMiningId",eiBlock.getCell( 0, "fdMiningId"));
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out; 				
}
	
	/**
	 * 删除方法
	 * <p>Function: delete</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCBCoalseam</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2013-6-26上午13:34:17 王开兵
	 * </p>
	 *
	 */
	@Override
	public EiInfo delete(EiInfo inInfo) 
	{
				EiInfo out=new EiInfo();
		ZMCHPlanValidateData _dom = new ZMCHPlanValidateData();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			
			Map<String, String> params = new HashMap<String, String>();
			params.put("fdKeyId", _dom.getFdKeyId());

		    this.dao.update("ZMCHPlanValidateData.delete", _dom);			
		}
		inInfo.set("fdForeignId",eiBlock.getCell( 0, "fdForeignId"));
		inInfo.set("fdMiningId",eiBlock.getCell( 0, "fdMiningId"));
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;
	}
	
	
	/**
	 * 生成调度记录
	 * <p>Function: report</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-7-6下午04:07:27 张志芳
	 * </p>
	 *
	 */
	public EiInfo generate(EiInfo inInfo)
	{
		EiInfo out=new EiInfo();
		ZMCHPlanValidateData _dom = new ZMCHPlanValidateData();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			ZMCGRecord _dom1 = new ZMCGRecord();
			
			_dom1.setFdKeyId(_dom.getFdKeyId());
//			_dom1.setFdWorknumId(_dom.getFdWorknumId());
//			_dom1.setFdVirtualDate(_dom.getFdVirtualDate());
//			_dom1.setFdCurrentDate(_dom.getFdCurrentDate());
//			_dom1.setFdLeaderName(_dom.getFdReportPerson());
//			_dom1.setFdImportRecord(_dom.getFdCheckCondition());
//			this.dao.insert("ZMCGRecord.insert",_dom1); 
		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out; 		
	
	}
} 